package de.Torgenizer.presentation.Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Closeable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Observable;

import javax.swing.JDialog;

import de.Torganzier.presentation.GUI.MainFrame;
import de.Torganzier.presentation.GUI.AddEmployeeDialog;
import de.Torgenizer.appli.controller.MittarbeiterControllerInterface;
import de.Torgenizer.persistence.POJO.Mittarbeiter;
import de.Torgenizer.persistence.POJO.Position;
import de.Torgenizer.presentation.Gui.Menu.MenuController;

public class MainFrameController extends Observable implements ActionListener {
	private MainFrame mf;
	private MittarbeiterControllerInterface mc;
	private MenuController menuController;
	
	public MainFrameController(MittarbeiterControllerInterface mc){
		mf = new MainFrame(this);
		this.mc = mc;
		this.menuController = new MenuController();
		mf.setMenu(this.menuController.getMenu());
		
		try {
			mf.setEmployeeList((ArrayList<Mittarbeiter>)this.mc.getAll());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	public void showMainFrame(){
		mf.pack();
		
		mf.setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == mf.getAddButton()){
			mf.shwoAddEmployeeDialog();
		}
		if(e.getSource() == mf.getAddEmployeeAbortClicked()){
			mf.closeAddEmployeeDialog();
		}
		if(e.getSource() == mf.getAddEmployeeOKClicked()){
			try {
				this.mc.insertMittarbeiter(mf.getAddEmplyeeFirstNameEntry(), mf.getAddEmployessLastNameEntry(),Position.Anästhesist);
				mf.setEmployeeList((ArrayList<Mittarbeiter>)this.mc.getAll());
				mf.closeAddEmployeeDialog();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
	}
}
